package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.csshlj.form.ShljYsForm;
import com.augurit.gzcgwsp.csshlj.service.IShljYsService;
import com.augurit.gzcgwsp.readAndParseXML.service.IHwShljYsService;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 解析生活垃圾清扫xml数据  水域
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional
public class HwShljYsServiceImpl implements IHwShljYsService {
	Logger logger = (Logger) LoggerFactory.getLogger(HwShljYsServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;
	
	@Autowired
	private IShljYsService shljYsService;
	
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 

	// xml存放路径
	private final String shljYsfilepath = localResource.getString("shljYsfilepath");

	// 附件存放路径
	private final String shljYsfujianpath = localResource.getString("shljYsfujianpath");

	// 解析成功反馈xml存放路径
	private final String shljYsfeedbackPath = localResource.getString("shljYsfeedbackPath");

	// 解析异常xml存放路径
	private final String shljYsexceptionPath = localResource.getString("shljYsexceptionPath");

	// 解析后，xml备份
	private final String shljYsbackPath = localResource.getString("shljYsbackPath");

	private Map<String, String> shljYsKeyMap;// xml内容的key

	private Map<String, String> shljYsKeyMapHead;// xml头部的key

	private Map<String, Long> shljYsfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void initshljYsMap() {
		// 初始化数据部分对应的map
		shljYsKeyMap = new HashMap<String, String>();
		shljYsKeyMap.put("YSL_NO", "YSL_NO");
		shljYsKeyMap.put("approvalNo", "approvalNo");
		shljYsKeyMap.put("sendTime", "sendTime");
		
		shljYsKeyMap.put("SLRQ", "SLRQ");
//		shljYsKeyMap.put("SQNRQS", "SQNRQS");
//		shljYsKeyMap.put("SQNRSJ", "SQNRSJ");
//		shljYsKeyMap.put("SQNRYS", "SQNRYS");
		shljYsKeyMap.put("QYMC", "QYMC");
		shljYsKeyMap.put("ZZJGDM", "ZZJGDM");
		shljYsKeyMap.put("FRDB", "FRDB");
		shljYsKeyMap.put("YYZZZCH", "YYZZZCH");		
		shljYsKeyMap.put("LXDH", "LXDH");
		shljYsKeyMap.put("CZDH", "CZDH");
		shljYsKeyMap.put("EMAIL", "EMAIL");
		shljYsKeyMap.put("YZBM", "YZBM");
		shljYsKeyMap.put("BGZS", "BGZS");//新增了这个  - 2.20
		shljYsKeyMap.put("ZCZB", "ZCZB");
//		shljYsKeyMap.put("YZZM", "YZZM");
		shljYsKeyMap.put("XMMC", "XMMC");
		shljYsKeyMap.put("WTDLR", "WTDLR");
		shljYsKeyMap.put("LXDH2", "LXDH2");
		shljYsKeyMap.put("CZDH2", "CZDH2");
		shljYsKeyMap.put("EMAIL2", "EMAIL2");
		shljYsKeyMap.put("BZ", "BZ");
		shljYsKeyMap.put("SQFWNR", "SQFWNR");
		
		
//		shljYsKeyMap.put("ACCEPT_NO", "ACCEPT_NO");
//		shljYsKeyMap.put("SJR", "SJR");
//		shljYsKeyMap.put("SARQ", "SARQ");
//		shljYsKeyMap.put("CBYW", "CBYW");
//		shljYsKeyMap.put("CBBM", "CBBM");
		// shljYsKeyMap.put("会办部门", "会办部门");
//		shljYsKeyMap.put("CPSJ", "CPSJ");
//		shljYsKeyMap.put("DFSJ", "DFSJ");

		

		// 初始化头部对应的map
		shljYsKeyMapHead = new HashMap<String, String>();
		shljYsKeyMapHead.put("sn", "sn");
		shljYsKeyMapHead.put("sender", "sender");
		// shljYsKeyMapHead.put("receiver", "receiver");
		shljYsKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		shljYsfujianMap = new HashMap<String, Long>();
		shljYsfujianMap.put("FJ_01", 366l);
		shljYsfujianMap.put("FJ_02", 3368l);
		shljYsfujianMap.put("FJ_03", 367l);
		shljYsfujianMap.put("FJ_04", 368l);
		shljYsfujianMap.put("FJ_05", 440l);
		shljYsfujianMap.put("FJ_06", 504l);
		shljYsfujianMap.put("FJ_07", 505l);
		shljYsfujianMap.put("FJ_08", 506l);
		shljYsfujianMap.put("FJ_09", 507l);
		shljYsfujianMap.put("FJ_10", 508l);
		shljYsfujianMap.put("FJ_11", 509l);
	}

	public void readXml(String projectPath) {
		if (shljYsKeyMap == null)
			this.initshljYsMap();
		File file = new File(shljYsfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {
			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(shljYsKeyMap, filePath,shljYsfeedbackPath, shljYsexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(shljYsKeyMapHead,files[i], shljYsfeedbackPath, shljYsexceptionPath);

						// 把数据部分保存到数据库
						ShljYsForm form = new ShljYsForm();
						for (String mapKey : shljYsKeyMap.keySet()) {							
							// TODO 把值放到form中
							form.setQymc(parseXMLService.getMapValue(shljYsKeyMap.get("QYMC")));
							
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(shljYsKeyMap.get("QYMC")),"shljys");
							if(ywComQyxxForm != null){
								form.setQybh(ywComQyxxForm.getId().toString());
							}
							
							
							form.setZzjgdm(parseXMLService.getMapValue(shljYsKeyMap.get("ZZJGDM")));
							form.setFrdb(parseXMLService.getMapValue(shljYsKeyMap.get("FRDB")));
							form.setYyzzzch(parseXMLService.getMapValue(shljYsKeyMap.get("YYZZZCH")));
							form.setLxdh(parseXMLService.getMapValue(shljYsKeyMap.get("LXDH")));
							form.setFrdb(parseXMLService.getMapValue(shljYsKeyMap.get("FRDB")));
							form.setLxdh(parseXMLService.getMapValue(shljYsKeyMap.get("LXDH")));
							form.setCzdh(parseXMLService.getMapValue(shljYsKeyMap.get("CZDH")));
							form.setEmail(parseXMLService.getMapValue(shljYsKeyMap.get("EMAIL")));
							form.setYzbm(parseXMLService.getMapValue(shljYsKeyMap.get("YZBM")));
							form.setBgzs(parseXMLService.getMapValue(shljYsKeyMap.get("BGZS")));//新增这个  - 2.20
							form.setZczb(parseXMLService.getMapValue(shljYsKeyMap.get("ZCZB")));
							form.setXmmc(parseXMLService.getMapValue(shljYsKeyMap.get("XMMC")));
							form.setWtdlr(parseXMLService.getMapValue(shljYsKeyMap.get("WTDLR")));
							form.setLxdh2(parseXMLService.getMapValue(shljYsKeyMap.get("LXDH2")));
							form.setCzdh2(parseXMLService.getMapValue(shljYsKeyMap.get("CZDH2")));
							form.setEmail2(parseXMLService.getMapValue(shljYsKeyMap.get("EMAIL2")));
							form.setBz(parseXMLService.getMapValue(shljYsKeyMap.get("BZ")));
							String sqfwnr = parseXMLService.getMapValue(shljYsKeyMap.get("SQFWNR"));
//							String sqfwnr = "传过来的值";
							if(StringUtils.isBlank(sqfwnr) || "null".equals(sqfwnr)){//为空，或者为null
								form.setSqnrqs("1");//没选择
								form.setSqnrsj("1");//没选择
								form.setSqnrys("1");//没选择
							}else{//有值而且不为  "null"
								if(sqfwnr.contains("1")){//选择了清扫
									form.setSqnrqs("0");//选择
								}else{
									form.setSqnrqs("1");//没选择
								}
								if(sqfwnr.contains("2")){//选择了收集
									form.setSqnrsj("0");//选择
								}else{
									form.setSqnrsj("1");//没选择
								}
								if(sqfwnr.contains("3")){//选择了运输
									form.setSqnrys("0");//选择
								}else{
									form.setSqnrys("1");//没选择
								}								
							}
							
							String SLRQTemp = parseXMLService.getMapValue(shljYsKeyMap.get("SLRQ"));
							Date SLRQ = new Date();
							if (org.apache.commons.lang.StringUtils.isNotEmpty(SLRQTemp)) {
								SLRQ = DateUtils.stringToDate(SLRQTemp,DateUtils.YYYY_MM_DD);
							}
							form.setSlrq(SLRQ);
							form.setCbyw(parseXMLService.getMapValue(shljYsKeyMap.get("CBYW")));
//							form.setCbbm(parseXMLService.getMapValue(shljYsKeyMap.get("CBBM")));
							form.setYslNo(parseXMLService.getMapValue(shljYsKeyMap.get("YSL_NO")));
							form.setApprovalNo(parseXMLService.getMapValue(shljYsKeyMap.get("approvalNo")));							
							form.setBusinessCode(parseXMLService.getMapValue(shljYsKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(shljYsKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setSendNotice("Y");
							form.setYsl(1l);
							form.setCbbm("环卫处");
							form.setCbyw("从事水域城市生活垃圾经营性清扫、收集、运输服务许可审批");
							
						}
						
						// 调用DAO，保存到数据库
						try {
							shljYsService.save(form);
//							shljYsService.autoStartProc(form, "shljys");
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									shljYsKeyMapHead, shljYsfeedbackPath, shljYsexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, shljYsfujianpath, shljYsfeedbackPath, shljYsexceptionPath, shljYsKeyMapHead,
								files, shljYsfujianMap, i, Integer.parseInt(form.getQybh()), form.getId(), "shljys");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								shljYsKeyMapHead, shljYsfeedbackPath, shljYsexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, shljYsbackPath + fileName,
							fileName);
				}
			}
		}
	}

}
